<template>
  <el-dialog
    title="获取料箱"
    :visible.sync="dialogFormVisible"
    width="50%"
    @close="back"
  >
    <div class="con" v-loading="!robotId" element-loading-text="正在呼叫小车中">
      <el-result icon="success" v-if="robotId" style="padding: 0px">
      </el-result>
      <div v-if="robotId" class="cont">
        <div style="color: #64c43c; font-size: 24px; margin: 10px 0px">
          已成功呼叫AMR
        </div>
        <div style="display: flex">
          <div style="font-size: 18px; margin: 20px 10px">
            任务编号:
            <span style="color: #dc041c; font-size: 22px">{{
              amrMissionId
            }}</span>
          </div>
          <div style="font-size: 18px; margin: 20px 10px">
            小车编号:
            <span style="color: #dc041c; font-size: 22px">{{ robotId }}</span>
          </div>
        </div>
        <!-- <div style="font-size: 18px; margin: 20px 0px">
          任务编号:
          <span style="color: #dc041c; font-size: 22px">{{
            amrMissionId
          }}</span>
        </div>
        <div style="font-size: 18px; margin: 20px 0px">
          小车编号:
          <span style="color: #dc041c; font-size: 22px">{{ robotId }}</span>
        </div> -->
        <el-input
          placeholder="请输入料箱编号"
          v-model="boxMark"
          style="width: 220px"
        ></el-input>
        <div style="margin-top: 20px">
          <el-button type="danger" @click="cancel" :loading="loading"
            >取消任务</el-button
          >
          <el-button type="primary" @click="bindLx" :loading="loading2"
            >绑定料箱</el-button
          >
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import {
  getEmptyBox,
  getAmrTask,
  cancelTask,
  bindBox,
  artificialAMR,
  sampleMove,
} from "@/common/api";

export default {
  name: "lx-Dialog",
  props: {},
  components: {},
  data() {
    return {
      dialogFormVisible: false,
      timer: null,
      loading: false,
      loading2: false,
      amrMissionId: "",
      robotId: "",
      boxMark: "",
      samNumber: "",
    };
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    getEmptyBox() {
      getEmptyBox().then((res) => {
        if (res.success) {
          this.amrMissionId = res.data.amrMissionId;
          this.selectInfo();
          this.timer = setInterval(() => {
            //根据任务编号轮询查小车信息
            this.selectInfo();
          }, 3000);
        } else {
          this.$errorMsg(res.message);
        }
      });
    },
    selectInfo() {
      getAmrTask({ id: this.amrMissionId }).then((res) => {
        if (res.success) {
          let json = JSON.parse(res.data);
          this.robotId = json.robot_id;
          if (this.robotId != 0) {
            this.close();
          }
        } else {
          //   this.$errorMsg(res.message);
        }
      });
    },
    cancel() {
      this.loading = true;
      cancelTask({ missionId: this.amrMissionId }).then((res) => {
        this.loading = false;
        if (res.success) {
          this.$successMsg("取消成功");
        } else {
          this.$errorMsg(res.message);
        }
      });
    },
    bindLx() {
      this.loading2 = true;
      //先绑定料箱，再释放小车，再样品流转
      bindBox({
        samNumber: this.samNumber,
        boxMark: this.boxMark,
      }).then((res) => {
        if (res.success) {
          this.outCar();
        } else {
          this.$errorMsg(res.message);
        }
      });
    },
    outCar() {
      artificialAMR({
        missionId: this.amrMissionId,
        targetCode: "1.4制样室停靠点",
      }).then((res) => {
        if (res.success) {
          this.ypTrans();
        } else {
          this.$errorMsg(res.message);
        }
      });
    },
    ypTrans() {
      sampleMove({
        samNumber: this.samNumber,
        boxMark: this.boxMark,
        reqRobot: this.robotId,
      }).then((res) => {
        this.loading2 = false;
        if (res.success) {
          this.$successMsg(res.message);
          this.dialogFormVisible = false;
        } else {
          this.$errorMsg(res.message);
        }
      });
    },

    back() {
      this.close();
    },
    close() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
  },
};
</script>


<style lang="scss" scoped>
.con {
  height: 280px;
  .cont {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
}
</style>
